package recallin.social.twitter;

import java.io.BufferedInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import oauth.signpost.OAuth;
import recallin.AndroindHttpClient.AndroidHttpClient;
import recallin.activities.MainActivity;
import twitter4j.ProfileImage;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.preference.PreferenceManager;

public class TwitterUtils {

	private static String screenName;
	private static String profileImageURL;
	public static AndroidHttpClient httpclient = null;
	

	public static void isAuthenticated(Activity activity) {
		
		final SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(activity);

		String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
		String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

		AccessToken a = new AccessToken(token, secret);
		final Twitter twitter = new Twitter();
		twitter.setOAuthConsumer(Constants.CONSUMER_KEY,
				Constants.CONSUMER_SECRET);
		twitter.setOAuthAccessToken(a);

		try {
			twitter.getAccountSettings();
			setScreenName(twitter.getScreenName());
			setProfileImageURL(twitter.getProfileImage(getScreenName(),
					ProfileImage.BIGGER).getURL());
			((MainActivity)activity).mHandler.sendEmptyMessage(MainActivity.TWITTER_DIALOG);
		} catch (TwitterException e) {
			((MainActivity)activity).mHandler.sendEmptyMessage(MainActivity.TWITTER_LOGIN);
		}
	}

	public static void sendTweet(Context context, String msg) throws Exception {
		final SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
		String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

		AccessToken a = new AccessToken(token, secret);
		Twitter twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer(Constants.CONSUMER_KEY,
				Constants.CONSUMER_SECRET);
		twitter.setOAuthAccessToken(a);
		twitter.updateStatus(msg);
		System.out.println(twitter.getFriendsIDs());
	}

	public static String getScreenName() {
		return screenName;
	}

	public static void setScreenName(String screenName) {
		TwitterUtils.screenName = screenName;
	}

	public static String getProfileImageURL() {
		return profileImageURL;
	}

	public static void setProfileImageURL(String profileImageURL) {
		TwitterUtils.profileImageURL = profileImageURL;
	}

	public static Bitmap getBitmap(String url) {
		Bitmap bm = null;
		try {
			URL aURL = new URL(url);
			URLConnection conn = aURL.openConnection();
			conn.connect();
			InputStream is = conn.getInputStream();
			BufferedInputStream bis = new BufferedInputStream(is);
			bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
			bis.close();
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (httpclient != null) {
				httpclient.close();
			}
		}
		return bm;
	}

	static class FlushedInputStream extends FilterInputStream {
		public FlushedInputStream(InputStream inputStream) {
			super(inputStream);
		}

		@Override
		public long skip(long n) throws IOException {
			long totalBytesSkipped = 0L;
			while (totalBytesSkipped < n) {
				long bytesSkipped = in.skip(n - totalBytesSkipped);
				if (bytesSkipped == 0L) {
					int b = read();
					if (b < 0) {
						break; // we reached EOF
					} else {
						bytesSkipped = 1; // we read one byte
					}
				}
				totalBytesSkipped += bytesSkipped;
			}
			return totalBytesSkipped;
		}
	}
}
